package com.dries.security.validate.code.captcha;

import com.dries.security.validate.code.captcha.enums.ValidateCodeType;
import com.dries.security.validate.code.exception.ValidateCodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description: 校验码处理器，封装不同校验码的处理逻辑
 * @Author ZSY
 * @createTime 2020/9/8 13:18
 */
@Slf4j
@Component
public class ValidateCodeProcessorHolder {

    @Resource
    private Map<String, Captcha> validateCodeProcessors;

    public Captcha findValidateCodeProcessor(ValidateCodeType codeType) {
        return findValidateCodeProcessor(codeType.toString().toLowerCase());
    }

    public Captcha findValidateCodeProcessor(String type) {
        String name = type.toLowerCase() + Captcha.class.getSimpleName();
        Captcha processor = validateCodeProcessors.get(name);
        if (processor == null) {
            throw new ValidateCodeException("验证码处理器" + name + "不存在");
        }
        return processor;
    }

}
